package com.wshl.aijiuunion.controller.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.wshl.aijiuunion.BaseActivity;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.utils.share.LoginOutUtils;
import com.wshl.aijiuunion.view.bean.PwdBean;
import com.wshl.aijiuunion.view.widget.NofastClickTextview;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created buy 风凊扬 on 2019/10/29 0029
 */
public class EditPwdActivity extends BaseActivity implements RequestModel.onSendcodeListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_login_phone)
    TextView tvLoginPhone;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.et_login_code)
    EditText etLoginCode;
    @BindView(R.id.tv_sendcode)
    NofastClickTextview tvSendcode;
    @BindView(R.id.tv_pwd)
    TextView tvPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.tv_confirm_pwd)
    TextView tvConfirmPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_setpwd;
    }

    private RequestModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        tvTopTitle.setText("修改支付密码");
        String mobile = SPUtils.getInstance().getString(GlobConstant.MOBILE);
        tvLoginPhone.setText(mobile);
        model = new RequestModel();
        model.setOnsendCodeListener(this);

    }

    @OnClick({R.id.img_back, R.id.tv_sendcode, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_sendcode:
                model.sendCode(EditPwdActivity.this, tvLoginPhone.getText().toString());
                break;
            case R.id.tv_confirm:

                String phone = tvLoginPhone.getText().toString();
                String code = etLoginCode.getText().toString().trim();
                String newpwd = etNewPwd.getText().toString().trim();
                String confirmPwd = etConfirmPwd.getText().toString().trim();

                PwdBean pwdBean = new PwdBean(phone, code, newpwd, confirmPwd);

                model.editPwd(EditPwdActivity.this, pwdBean, new RequestCallback() {
                    @Override
                    public void onSuccess(String response) {

                        LogUtils.i("修改密码:" + response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.has("code")) {

                                String code1 = jsonObject.getString("code");
                                switch (code1) {
                                    case GlobConstant.REQUESTSUCCESS:

                                        ToastUtils.showShort("修改成功");
                                        EditPwdActivity.this.finish();
                                        break;
                                    case GlobConstant.REQUESTERROR:
                                        LoginOutUtils.reLogin(EditPwdActivity.this);
                                        break;
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onError(int code) {

                    }

                    @Override
                    public void onIntentError() {

                    }
                });

                break;
        }
    }


    @Override
    public void start(int s) {

        tvSendcode.setClickable(false);
        tvSendcode.setText(s + "s");

    }

    @Override
    public void end() {

        tvSendcode.setClickable(true);
        tvSendcode.setText("重新发送");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelAll();
    }
}
